import  sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QtimerDemo(QWidget):
    def __init__(self,parent=None):
        super(QtimerDemo,self).__init__(parent)
        self.setWindowTitle("Qtimer案例")
        self.label=QLabel("显示当前时间")
        self.startBtn=QPushButton("开始")
        self.endBth=QPushButton("结束")
        layout=QGridLayout(self)
        #初始化一个定时器
        self.timer=QTimer(self)
        self.timer.timeout.connect(self.showTime)
        layout.addWidget(self.label,0,0,1,2)
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBth,1,1)
        self.startBtn.clicked.connect(self.startimer)
        self.endBth.clicked.connect(self.endTimer)
        self.setLayout(layout)
    def showTime(self):
        time=QDateTime.currentDateTime()
        #设置系统显示格式
        timeDisplay=time.toString("yyyy-MM-dd hh:mm:ss dddd")
        #在标间上显示时间
        self.label.setText(timeDisplay)
    def startimer(self):
        self.timer.start(1000)
        self.startBtn.setEnabled(False)
        self.endBth.setEnabled(True)
    def endTimer(self):
        self.timer.stop()
        self.startBtn.setEnabled(True)
        self.endBth.setEnabled(False)




if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=QtimerDemo()

    demo.show()
    sys.exit(app.exec_())
